class BlockController extends UDKBot;

var float BlockRadius;
var Pawn playerPawn;
var float StartSeconds;
var float blockinTime;
var float blockCooldown;

DefaultProperties
{
	bIsPlayer = true;
	bAdjustFromWalls = true;
	BlockRadius = 500;
	blockinTime = 0.50;
	blockCooldown = 1;
}

state Block
{
	event Tick(float deltatime){
		if( WorldInfo.TimeSeconds > (StartSeconds+blockinTime))
			Done();
	}

	Begin:
		`log('block');
		if(WorldInfo.TimeSeconds >= (StartSeconds+blockinTime+blockCooldown)){
			//LookAtPlayer();
			FightPawn(Pawn).PlayBlockAnimation();
			StartSeconds = WorldInfo.TimeSeconds;
		}
}

function bool CanBlock(){
	return (WorldInfo.TimeSeconds >= (StartSeconds+blockinTime+blockCooldown));
}



/**
 * Controleer of de bot aangevallen wordt
 */
function bool CheckAttack()
{
	local Projectile p;
	local float distance;
	foreach WorldInfo.AllActors(class'Projectile', p)
	{
		distance = VSize( Pawn.Location - p.Location );
		if(distance <= BlockRadius) return true;
	}
	return false;
}

function LookAtPlayer()
{
	if(playerPawn == none)
		FindNearestEnemy();
	Pawn.SetRotation(Rotator(playerPawn.Location - Pawn.Location));
}

function FindNearestEnemy()
{
	local Controller C;
	local float prevDistance,currentDistance;
	local bool first;

	first = true;

	foreach WorldInfo.AllControllers(class'Controller', C)
	{	
		
		if (C.bIsPlayer && !C.IsDead() && C != self)
		{
			currentDistance = VSize(Location - C.Pawn.Location);
			if( currentDistance < prevDistance || first)
			{
				if(FastTrace(Location,C.Pawn.Location))
				{
					first = false;
					prevDistance = currentDistance;
					playerPawn = C.Pawn;
				}
			}
		}
	}
}

function Done()
{
	//Geef aan klaar te zijn met de state
}